import sys, os, inspect, urllib
sys.dont_write_bytecode = True

# Add current directory to path
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)

import includes.page_header
import includes.page_login
import includes.page_footer
import includes.page_tab_bar
import includes.tab_main
import includes.tab_security
import includes.tab_job_history
import includes.tab_question_summary
import includes.tab_publications
import includes.tab_qa
import includes.tab_qualifications
import includes.tab_sales
from beaker.middleware import SessionMiddleware
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options


def askalien(environ, start_response):
    # Gets POST data from wsgi, into The_Form
    The_Form = ''
    try:
      length = int(environ.get('CONTENT_LENGTH', '0'))
    except ValueError:
      length = 0

    if length != 0:
      The_Form = environ['wsgi.input'].read(length)
      #The_Form = parsePOST(The_Form)
    
    session = environ['beaker.session']
    title   = "Ask Aliens"
    

    # Check to see if a value is in the session
    if 'user-id' not in session:
       output  = includes.page_header.get_contents(True) % title
       output += includes.page_login.get_contents()
       
    else:
        output  = includes.page_header.get_contents(False) % title
        output += includes.page_tab_bar.get_contents()
        output += '''
    <!-- Tab content -->

    <div>
    '''
    
        output += includes.tab_main.get_contents()
        output += includes.tab_security.get_contents()
        output += includes.tab_job_history.get_contents()
        output += includes.tab_publications.get_contents()
        output += includes.tab_question_summary.get_contents()
        output += includes.tab_qa.get_contents()
        output += includes.tab_qualifications.get_contents()
        output += includes.tab_sales.get_contents()
    
        output += '''
    </div>
    '''


        output += includes.page_footer.get_contents()
    start_response('200 OK', [('Content-type', 'text/html')])
    return [output]

# Configure the SessionMiddleware
session_opts = {
    'session.type': 'file',
    'session.timeout' : 3600,
    'session.cookie_expires': True,
    'session.data_dir' :  '/tmp/cache/data'
}

application = SessionMiddleware(askalien, session_opts)


#cache_opts = {
#    'cache.type': 'file',
#    'cache.data_dir': '/tmp/cache/data',
#    'cache.lock_dir': '/tmp/cache/lock'
#}

#cache = CacheManager(**parse_cache_config_options(cache_opts))

